package com.gluck.cases.core.block.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gluck.cases.common.utils.enums.BizSerErrorEnum;
import com.gluck.cases.common.utils.dal.dao.RolePermissionMapper;
import com.gluck.cases.common.utils.utils.AssertUtil;
import com.gluck.cases.core.block.RolePermissionBlock;
import com.gluck.cases.core.modal.assemble.RolePermissionAssemble;
import com.gluck.cases.core.modal.dto.RolePermissionDTO;
import com.gluck.cases.core.modal.entity.RolePermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @ClassName RolePermissionBlockImpl
 * @Desc RolePermissionBlockImpl
 * @Author I am Gluck
 * @Date 2022/5/20 - 12:49 上午
 **/
@Service
public class RolePermissionBlockImpl implements RolePermissionBlock {

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public RolePermission relatedPermissionByRole(RolePermissionDTO dto) {
        RolePermission rolePermission = rolePermissionMapper.selectByRoleIdAndPemissionId(RolePermissionAssemble.rolePermissionDTOConverRolePermission(dto));
        AssertUtil.isTrue(Objects.isNull(rolePermission) ? true : false, BizSerErrorEnum.DATA_NOT_FOUND);
        int insert = rolePermissionMapper.insert(rolePermission);
        AssertUtil.isTrue(insert == 0 ? true : false, BizSerErrorEnum.INSERT_ERROR);
        return rolePermission;
    }

    @Override
    public Integer disassociatePermissionByRole(RolePermissionDTO dto) {
        RolePermission rolePermission = rolePermissionMapper.selectByRoleIdAndPemissionId(RolePermissionAssemble.rolePermissionDTOConverRolePermission(dto));
        AssertUtil.isTrue(Objects.isNull(rolePermission) ? true : false, BizSerErrorEnum.DATA_NOT_FOUND);
        int insert = rolePermissionMapper.updateByPrimaryKeySelective(rolePermission);
        AssertUtil.isTrue(insert == 0 ? true : false, BizSerErrorEnum.DISASSOCIATE_PERMISSION_BY_ROLE_ERROR);
        return insert;
    }

    @Override
    public PageInfo<RolePermission> selectByPagerHelper(Integer startPage, Integer endPage) {
        PageInfo<RolePermission> page = new PageInfo<>(PageHelper.startPage(startPage, endPage).doSelectPage(() -> rolePermissionMapper.selectAll()));
        return page;
    }
}
